<?php

namespace app\api\controller\mox;

use app\common\controller\Api;
use app\admin\model\mox\inherit\story\Info as InfoModel;
use app\admin\model\mox\inherit\story\Chapter as ChapterModel;
use app\admin\model\mox\inherit\story\Label as LabelModel;


/**
 * 故事
 */
class Story extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 获取故事标签
     */
    public function type()
    {
        $this->success('获取成功', LabelModel::select());
    }
    /**
     * 故事列表
     *
     */
    public function list()
    {
        $title = $this->request->param('title');
        $label_id = $this->request->param('type_id');
        $model = new InfoModel();
        $where = [];
        if ($label_id != 0) {
            $model->whereRaw("FIND_IN_SET(?, label_id)", [$label_id]);
        }
        // 2. 按标题模糊查询
        if ($title != null) {
            $model->whereRaw("title LIKE ?", ["%{$title}%"]);
        }
        $list = $model->order('id desc')
            ->where($where)
            ->field('id, title, image,label_id,brief_introduction,create_time')
            ->paginate()
            ->each(function ($item, $key) {
                //获取标签
                $label_id = explode(',', $item['label_id']);
                $item['label'] = model('app\admin\model\mox\inherit\story\Label')->where('id', 'in', $label_id)->field('id, title,color')->select();
            });

        $this->success('请求成功', $list);
    }
    /**
     * 故事详情
     */
    public function storyDetail()
    {
        $id = $this->request->param('id');
        if (!$id) {
            $this->error('缺少参数id');
        }
        $model = new InfoModel();
        $info = $model->where('id', $id)->find();
        if (!$info) {
            $this->error('故事不存在');
        }
        $info['user'] = model('app\common\model\User')->where('id', $info['user_id'])->field('id, nickname, avatar')->find();
        $label_id = explode(',', $info['label_id']);
        $info['label'] = model('app\admin\model\mox\inherit\story\Label')->where('id', 'in', $label_id)->select();
        $info['chapter'] = model('app\admin\model\mox\inherit\story\Chapter')->where('s_id', $id)->limit(5)->select();
        $info['chapter_num'] = count($info['chapter']);
        $related_persons = explode(',', $info['related_persons']);
        $info['related_persons'] = model('app\admin\model\mox\inherit\Character')->where('id', 'in', $related_persons)->field('id, name,image')->select();
        //相关剧目
        $info['play'] = model('app\admin\model\mox\play\Info')->where('s_id', $id)->field('id, title, image,type_id,brief_introduction,create_time,update_time')->limit(5)->select();
        foreach ($info['play'] as $key => $value) {
            $value->type = model('app\admin\model\mox\play\Type')->where('id', $value['type_id'])->field('id, title')->find();
        }
        $this->success('请求成功', $info);
    }


    /**
     * 章节列表
     */
    public function chapterList()
    {
        $s_id = $this->request->param('s_id');
        if (!$s_id) {
            $this->error('缺少参数s_id');
        }
        $model = new ChapterModel();
        $list = $model->where('s_id', $s_id)->order('id asc')->select();
        $this->success('请求成功', $list);
    }

    /**
     * 章节详情
     */
    public function chapterDetail()
    {
        $id = $this->request->param('id');
        if (!$id) {
            $this->error('缺少参数id');
        }
        $model = new ChapterModel();
        $info = $model->where('id', $id)->find();

        $fileName = md5(md5($id) .  $info['s_id']);
        $file_name = "public/uploads/mox_story/" .  $info['s_id'] . "/" . $fileName . ".txt";
        $saveDir = $path = ROOT_PATH;
        $filePath =  $saveDir .  $file_name;
        if (file_exists($filePath)) {
            $content = file_get_contents($filePath);
            $info['content'] = $content;
        } else {
            $info['content'] = '';
        }
        if (!$info) {
            $this->error('章节不存在');
        }
        $this->success('请求成功', $info);
    }
}
